<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Models\Product as ProductModel;

class ProductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Product(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->title;
            $grid->image->image('', 75);
            $grid->on_sale->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->price;
            $grid->ideal_price;
            $grid->minimum_markup;
            $grid->start_at->sortable();
            $grid->end_at->sortable();
            $grid->created_at->sortable()->hide();
            $grid->updated_at->sortable()->hide();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('title');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {
            $show->id;
            $show->title;
            $show->description;
            $show->image;
            $show->on_sale;
            $show->price;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Product(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->editor('description');
            $form->image('image');
            $form->radio('on_sale')->options(ProductModel::$saleMap)->default(true);
            $form->decimal('price');
            $form->decimal('ideal_price');
            $form->decimal('minimum_markup');

            $form->datetime('start_at');
            $form->datetime('end_at');
        });
    }
}
